##=============================================================================
## Appendix: Figure 17
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "hrbrthemes", "cobalt")

ipak(packages)

##---------
# Load data
#setwd("~/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

test1 <- dat %>% 
  select(id, wave, save_lives, survival_mindset, sex, age, edu, job, finances, 
         home_lang, ethnic, rus_ethnic, displaced) %>% 
  group_by(wave) %>% 
  filter(is.na(save_lives) & is.na(survival_mindset)) %>% 
  mutate(var = "Missing Both")

test2 <- dat %>% 
  select(id, wave, save_lives, survival_mindset, sex, age, edu, job, finances, 
         home_lang, ethnic, rus_ethnic, displaced) %>% 
  group_by(wave) %>% 
  filter(!is.na(save_lives) & !is.na(survival_mindset)) %>% 
  mutate(var = "Complete Cases")

test3 <- dat %>% 
  select(id, wave, save_lives, survival_mindset, sex, age, edu, job, finances, 
         home_lang, ethnic, rus_ethnic, displaced) %>% 
  group_by(wave) %>% 
  filter(!is.na(save_lives) & is.na(survival_mindset)) %>% 
  mutate(var = "Missing `Survival Mindset`")

test4 <- dat %>% 
  select(id, wave, save_lives, survival_mindset, sex, age, edu, job, finances, 
         home_lang, ethnic, rus_ethnic, displaced) %>% 
  group_by(wave) %>% 
  filter(is.na(save_lives) & !is.na(survival_mindset)) %>% 
  mutate(var = "Missing `Save Lives`")

dat3 <- test1 %>% 
  bind_rows(test2) %>% 
  bind_rows(test3) %>% 
  bind_rows(test4) %>% 
  mutate(wave = case_when(
    wave == 0 ~ "July 2022",
    wave == 1 ~ "February 2023")) %>% 
  mutate(wave = factor(wave)) %>% 
  mutate(var = factor(var)) %>% 
  mutate(hs_edu = ifelse(edu > 4, 1, 0),
         poor = ifelse(finances < 3, 1, 0),
         ukr_home_lang = ifelse(home_lang == 1, 1, 0),
         treat1 = ifelse(var == "Missing `Save Lives`", 1, 0),
         treat2 = ifelse(var == "Missing `Survival Mindset`", 1, 0),
         treat3 = ifelse(var == "Missing Both", 1, 0)) %>% 
  ungroup() %>% 
  mutate(treat = case_when(
    var == "Complete Cases" ~ 0,
    var == "Missing `Save Lives`" ~ 1,
    var == "Missing `Survival Mindset`" ~ 2,
    var == "Missing Both" ~ 3)) %>% 
  select(id, wave, treat, sex, age, poor, displaced, ukr_home_lang, rus_ethnic, 
         hs_edu)
    
rm(test1, test2, test3, test4)

dat_missing_save_lives <- dat3 %>%
  select(treat, sex, age, poor, displaced, ukr_home_lang, rus_ethnic, hs_edu) %>% 
  filter(treat == 0 | treat == 1) %>% 
  mutate(treat = ifelse(treat == 0, 0, 1)) %>% 
  drop_na()

obs_balance <- bal.tab(dat_missing_save_lives %>% select(sex:hs_edu), 
                       treat = dat_missing_save_lives$treat)

obs_balance <- obs_balance$Balance
obs_balance$var <- rownames(obs_balance)

obs_balance_1 <- obs_balance %>% 
  select(var, type = Type, smd = Diff.Un) %>%
  as_tibble() %>%
  mutate(data = "Unweighted") %>% 
  filter(var == "sex" | var == "age" | var == "hs_edu" | var == "poor" | 
           var == "rus_ethnic" | var == "ukr_home_lang" | var == "displaced") %>% 
  mutate(abs_smd = abs(smd),
         samp = "Missing `Save Lives`") %>% 
  dplyr::select(samp, var, abs_smd)

rm(obs_balance, dat_missing_save_lives)

dat_missing_survival_mindset <- dat3 %>% 
  filter(treat == 0 | treat == 2) %>% 
  mutate(treat = ifelse(treat == 0, 0, 1)) %>% 
  select(treat, sex, age, poor, displaced, ukr_home_lang, rus_ethnic,
         hs_edu) %>% 
  drop_na()

obs_balance <- bal.tab(dat_missing_survival_mindset %>% select(sex:hs_edu), 
                       treat = dat_missing_survival_mindset$treat)

obs_balance <- obs_balance$Balance
obs_balance$var <- rownames(obs_balance)

obs_balance_2 <- obs_balance %>% 
  select(var, type = Type, smd = Diff.Un) %>%
  as_tibble() %>%
  mutate(data = "Unweighted") %>% 
  filter(var == "sex" | var == "age" | var == "hs_edu" | var == "poor" | 
           var == "rus_ethnic" | var == "ukr_home_lang" | var == "displaced") %>% 
  mutate(abs_smd = abs(smd),
         samp = "Missing `Survival Mindset`") %>% 
  dplyr::select(samp, var, abs_smd)

rm(obs_balance, dat_missing_survival_mindset)


dat_missing_both <- dat3 %>% 
  filter(treat == 0 | treat == 3) %>% 
  mutate(treat = ifelse(treat == 0, 0, 1)) %>% 
  select(treat, sex, age, poor, displaced, ukr_home_lang, rus_ethnic,
         hs_edu) %>% 
  drop_na()

obs_balance <- bal.tab(dat_missing_both %>% select(sex:displaced), 
                       treat = dat_missing_both$treat)
obs_balance <- obs_balance$Balance
obs_balance$var <- rownames(obs_balance)

obs_balance_3 <- obs_balance %>% 
  select(var, type = Type, smd = Diff.Un) %>%
  as_tibble() %>%
  mutate(data = "Unweighted") %>% 
  filter(var == "sex" | var == "age" | var == "hs_edu" | var == "poor" | 
           var == "rus_ethnic" | var == "home_lang" | var == "displaced") %>% 
  mutate(abs_smd = abs(smd),
         samp = "Missing Both") %>% 
  dplyr::select(samp, var, abs_smd)

rm(obs_balance, dat_missing_both)

obs_balance <- obs_balance_1 %>% 
  bind_rows(obs_balance_2) %>% 
  bind_rows(obs_balance_3)

rm(obs_balance_1, obs_balance_2, obs_balance_3)

obs_balance <- obs_balance %>% 
  mutate(var_name = case_when(
    var == "sex" ~ "Sex",
    var == "age" ~ "Age (Years)",
    var == "hs_edu" ~ "H.S. Edu. or Higher",
    var == "poor" ~ "Poverty",
    var == "rus_ethnic" ~ "Russian-identifying",
    var == "ukr_home_lang" ~ "Ukr. Home Lang.",
    var == "displaced" ~ "Displaced"))

ggplot(obs_balance, aes(x = abs_smd, y = fct_reorder(var_name, abs_smd), 
                        shape = samp)) +
  geom_point(size = 3) +
  scale_x_continuous(n.breaks = 10, limits = c(0, 1)) +
  geom_rect(aes(xmin = 0, xmax = 0.10, ymin = -Inf, ymax = Inf),
            alpha = 0.05) +
  labs(x = "Absolute Standardized Mean Difference", y = "", shape = "") +
  theme_sjplot(base_size = 18) +
  theme(legend.position = "bottom",
        panel.grid.minor = element_blank())

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================